/*
 * DATA GENERATOR.C
 * 
 * Program do generowania binarnych danych potrzebnych do testowania instrukcji procesora AVR.
 * 
 */


#include <stdio.h>

typedef unsigned char uint8_t;

#define MAX_ADDRESS 		0xFFFF
#define BEGIN_REG_MEM_ADDR 	0x0000
#define END_REG_MEM_ADDR 	0x001F
#define BEGIN_IO_MEM_ADDR 	0x0020
#define END_IO_MEM_ADDR 	0x005F
#define BEGIN_INT_SRAM_ADDR 	0x0060
#define END_INT_SRAM_ADDR 	0x0FFF
#define BEGIN_EXT_SRAM_ADDR 	0x1000
#define END_EXT_SRAM_ADDR 	0xFFFF

uint8_t MEMD[MAX_ADDRESS+1];


void saveMEMD(char *file){        //zapisz zawarto�� pami�ci danych do pliku
    FILE *file_ptr;
    file_ptr=fopen(file, "wb");
    if(!file_ptr){
        printf("MEMD cannot open to write (%s)!\n", file);
        exit(1);
    }
    fseek(file_ptr, 0, SEEK_SET);
    fwrite(MEMD, MAX_ADDRESS+1, 1, file_ptr);
    fclose(file_ptr);
}

void loadMEMD(char *file){        //�adowanie pami�ci danych z pliku
     FILE *file_ptr;
    file_ptr=fopen(file, "rb");
    if(!file_ptr){
        printf("MEMD file not found (%s)!\n", file);
        exit(2);
    }
    fseek(file_ptr, 0, SEEK_SET);
    fread(MEMD, MAX_ADDRESS+1, 1, file_ptr);
    fclose(file_ptr);
}

int main(int argc, char *argv[]){
  loadMEMD(argv[1]);
  int i = 0;

  
  for(i = BEGIN_REG_MEM_ADDR; i <= END_REG_MEM_ADDR; i++) MEMD[i] = 0;
  printf("Koniec pamięci rejestrowej\n");
  
  printf("Początek pamięci I/O\n");
  for(i = BEGIN_IO_MEM_ADDR ; i <= END_IO_MEM_ADDR; i++) MEMD[i] = 0;
  printf("Koniec pamięci IO\n");
  printf("Początek pamięci Internal SRAM\n");
  for(i = BEGIN_INT_SRAM_ADDR ; i <= END_INT_SRAM_ADDR; i++) MEMD[i] = 0;
  printf("Koniec pamięci Internal SRAM\n");
  printf("Początek pamięci External SRAM\n");
  for(i = BEGIN_EXT_SRAM_ADDR ; i <= END_EXT_SRAM_ADDR; i++) MEMD[i] = 0;
  printf("Koniec pamięci External SRAM\n");

  saveMEMD(argv[1]);
  
  return 0;
}
  
  
